//
//  VideoPlayerLayerManagerType.swift
//  VideoPlayer
//
//  Created by JY_NEW on 2021/10/11.
//

import Foundation

/// 播放器浮层管理器
public protocol VideoPlayerLayerManagerType: AnyObject {
    /// 实现时需使用weak，用于控制播放器
    var playerManager: VideoPlayerManagerType? { get set }
    /// 事件通知统一管理
    var observer: VideoPlayerLayerManagerObserverType { get }
    /// 当前添加的全部层
    var layers: [VideoPlayerLayerType] { get }
    /// 图层是否锁住(只影响控制层)
    var isControlLocked: Bool { get set }
    /// 添加层，返回标识符
    @discardableResult
    func addLayer(_ layer: VideoPlayerLayerType) -> Int
    /// 移除层
    func removeLayer(_ identifier: Int)
    /// 移除层
    func removeLayer<LayerType: VideoPlayerLayerType>(for type: LayerType.Type)
    
    /// 激活指定层
    func activeLayer(_ layer: VideoPlayerLayerType)
    /// 反激活指定层
    func deactiveLayer(_ layer: VideoPlayerLayerType)
}

// MARK: 跟层交互操作

extension VideoPlayerLayerType {
    /// 激活该层
    public func active() {
        /// 若本身已经激活了则不重复操作
        if self.status.isActived { return }
        if self.isDisabled { return }
        self.willActive()
        self.manager?.activeLayer(self)
        // 激活时自动暂停
        if let pauseFlag = self.autoPauseFlag {
            self.playerManager?.pause(for: pauseFlag)
        }
        self.didActived()
    }
    /// 反激活该层
    public func deactive() {
        /// 若本身已经反激活了则不重复操作
        if !self.status.isActived { return }
        if self.isDisabled { return }
        self.willDeactive()
        self.manager?.deactiveLayer(self)
        // 反激活时自动播放原先的暂停
        if let pauseFlag = self.autoPauseFlag {
            self.playerManager?.play(for: pauseFlag)
        }
        self.didDeactived()
    }
    public func willActive() {
        
    }
    public func willDeactive() {
        
    }
    public func didActived() {
        
    }
    public func didDeactived() {
        
    }
    /// 是否允许控制
    public var isAllowControl: Bool {
        if let lock = self.manager?.isControlLocked, !lock {
            return true
        }
        return false
    }
}


// MARK: 辅助操作

extension VideoPlayerLayerManagerType {
    /// 根据标识获取浮层
    public func layer(for identifier: Int) -> VideoPlayerLayerType? {
        return self.layers.first(where: { $0.identifier == identifier })
    }
    /// 根据类型获取浮层
    public func layer<LayerType: VideoPlayerLayerType>(for type: LayerType.Type) -> LayerType? {
        return self.layers.first(where: { $0 is LayerType }) as? LayerType
    }
    /// 根据类型获取浮层
    public func layers<LayerType: VideoPlayerLayerType>(for type: LayerType.Type) -> [LayerType] {
        return self.layers.compactMap({ $0 as? LayerType })
    }
}
extension Array where Element == VideoPlayerLayerType {
    /// 获取全部插件层
    public var plugins: Self {
        return self.filter({ $0.category == .plugin })
    }
    /// 获取全部状态层
    public var states: Self {
        return self.filter({ $0.category == .state })
    }
    /// 获取全部控制层
    public var controls: Self {
        return self.filter({ $0.category == .control })
    }
    /// 获取全部遮挡层
    public var blocks: Self {
        return self.filter({ $0.category == .block })
    }
    /// 获取全部激活的层
    public var actived: Self {
        return self.filter({ $0.status.isActived })
    }
    /// 获取全部反激活的层
    public var deactived: Self {
        return self.filter({ !$0.status.isActived })
    }
    /// 获取全部显示的层
    public var showed: Self {
        return self.filter({ $0.status.isShowed })
    }
    public func active() {
        self.forEach({ $0.active() })
    }
    public func deactive() {
        self.forEach({ $0.deactive() })
    }
}
